package com.test.base.controller;


import com.mysql.cj.util.StringUtils;
import com.test.base.entity.User;
import com.test.base.service.UserService;
import com.test.utils.ApplicationContextUtil;
import com.test.utils.SaltUtil;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author ssang
 * @since 2021-12-09
 */
@Controller
@RequestMapping("/user")
public class UserController {

    @Autowired
    UserService userService;

    @RequestMapping("/test")
    public void test(){
        Object userService = ApplicationContextUtil.getBean("userServiceImpl");
        System.out.println(userService.toString());
    }

    @PostMapping("/register")
    public String register(User user){

        if(StringUtils.isNullOrEmpty(user.getUsername()) || StringUtils.isNullOrEmpty(user.getPassword())){
            return "register";
        }

        String salt = SaltUtil.getRandomSalt(5);

        String password = user.getPassword();
        String newPassword = new Md5Hash(password,salt,1024).toHex();
        user.setPassword(newPassword);
        user.setSalt(salt);

        userService.save(user);
        return "login";
    }
}
